'''
Created on Jan 25, 2012

@author: onetera
'''
from PyQt4 import QtCore , QtGui
from lib.papyDB import papyDB , tableInfo
from lib import globalVar
from lib import papyDBTable
from shotAssembler_UI import shotAssembler_UI
from insertItem import insertItemDiag
from lib.itemModel import treeItem
from lib.customUI import customUI
import os
import pickle
papyDB = papyDB()



    
class treeModel( QtCore.QAbstractItemModel ):    
    def __init__(self , headers ):
        super(treeModel, self).__init__()       
        self.rootItem = treeItem( 'root' )        
        self.setModelData( )
        
    def setModelData(self):  
        prjList = papyDBTable.PROJECT.search( 'prjCodeName' )      
        prjItems = [ treeItem( prj ) for prj in prjList ]        
        for prjCode , prjItem in zip( prjList , prjItems ):
            prjID = papyDBTable.PROJECT.search( 'prjID' , prjCodeName = prjCode )            
            seqList = papyDBTable.SEQUENCES.search( 'seqName' , prjID = prjID  )            
            seqItems = [ treeItem( seq ) for seq in seqList ]            
            prjItem.childItems.extend( seqItems )            
            for seqItem , seqName in zip( seqItems , seqList ) :
                seqItem.parentItem = prjItem                
                seqID = papyDBTable.SEQUENCES.search( 'seqID' , seqName = seqName , prjID = prjID )                
                shotList = papyDBTable.SHOT.search( 'shotName' , seqID = seqID )
                shotItems = [ treeItem( shot )  for shot in shotList ]
                seqItem.childItems.extend( shotItems )
                for shot in shotItems:
                    shot.parentItem = seqItem            
        self.rootItem.childItems.extend( prjItems )                                       
                        
    def columnCount(self, parent=QtCore.QModelIndex()):
        return self.rootItem.columnCount()        
    
    def data(self, index, role):
        if not index.isValid():
            return None
        if role != QtCore.Qt.DisplayRole and role != QtCore.Qt.EditRole:
            return None
        item = self.getItem(index)
        return item.data()
        
    def getItem(self, index):
        if index.isValid():
            item = index.internalPointer()
            if item:
                return item
        return self.rootItem
    
    def index(self, row, column, parent=QtCore.QModelIndex()):
        if parent.isValid() and parent.column() != 0:
            return QtCore.QModelIndex()
        parentItem = self.getItem(parent)
        childItem = parentItem.child(row)
        if childItem:
            return self.createIndex(row, column, childItem)
        else:
            return QtCore.QModelIndex()
    
    def parent(self, index):
        if not index.isValid():
            return QtCore.QModelIndex()
        childItem = self.getItem(index)
        parentItem = childItem.parent()
        if parentItem == self.rootItem:
            return QtCore.QModelIndex()
        elif parentItem == None :
            return QtCore.QModelIndex()
        return self.createIndex(parentItem.childNumber(), 0, parentItem )
    
    def rowCount(self, parent=QtCore.QModelIndex()):
        parentItem = self.getItem(parent)
        return parentItem.childCount()
        
class treeViewer( QtGui.QMainWindow ):
    def __init__(self , parent = None ):
        QtGui.QMainWindow.__init__( self , parent )
        self.treeItemModel = treeModel('test')
        
    def showWin(self):        
        self.treeViewer = QtGui.QTreeView( self )
        self.treeViewer.setModel( self.treeItemModel )
        self.mainWidget = QtGui.QWidget( )
        gLayout = QtGui.QGridLayout( self.mainWidget )
        gLayout.addWidget( self.treeViewer )
        self.setCentralWidget( self.mainWidget )        
        self.show()

        
if __name__ == '__main__':     
    import sys    
    app = QtGui.QApplication(sys.argv)
    treeViewer = treeViewer()
    treeViewer.showWin()
    app.exec_()     

   